package com.genghis.rapidcrm.kernel;

import java.util.List;

public class RapidPagingModel {

    private static final int DEFAULT_PAGE = 1;
    private static final int PAGE_LIMIT = 20;

    private Integer page;
    private Integer limit;
    private RangeInfo rangeInfo;
    private Boolean prevPageExists;
    private Boolean nextPageExists;
    private String viewId;

    public void setPage(Integer page) {
        this.page = page;
    }

    public void setLimit(Integer limit) {
        this.limit = limit;
    }

    public String getViewId() {
        return viewId;
    }

    public void setViewId(String viewId) {
        this.viewId = viewId;
    }

    /**
     * Function to get the current page number
     *
     * @return <Number>
     */
    public Integer getCurrentPage() {
        if (this.page != null && this.page > 0) {
            return this.page;
        } else {
            return DEFAULT_PAGE;
        }
    }

    /**
     * Function to get the Next page number
     *
     * @return <Number>
     */
    public Integer getNextPage() {
        int currentPage;
        if (this.page != null && this.page > 0) {
            currentPage = this.page;
        } else {
            currentPage = DEFAULT_PAGE;
        }
        return currentPage + 1;
    }

    /**
     * Function to get the limit on the number of records per page
     *
     * @return <Number>
     */
    public Integer getPageLimit() {
        int pageLimit;
        if (this.limit != null && this.limit > 0) {
            pageLimit = this.limit;
        } else {
//            $pageLimit = vglobal('list_max_entries_per_page');
            pageLimit = PAGE_LIMIT;
        }
        return pageLimit;
    }

    public Integer getStartIndex() {
        int currentPage = this.getCurrentPage();
        int pageLimit = this.getPageLimit();
        return (currentPage - 1) * pageLimit;
    }

    /**
     * Retrieves start sequence number of records in the page
     *
     * @return <Integer>
     */
    private Integer getRecordStartRange() {
        RangeInfo rangeInfo = this.getRecordRange();
        return rangeInfo.getStartIndex();
    }

    /**
     * Retrieves end sequence number of records in the page
     *
     * @return <Integer>
     */
    private Integer getRecordEndRange() {
        RangeInfo rangeInfo = this.getRecordRange();
        return rangeInfo.getEndIndex();
    }

    /**
     * Retrieves start and end sequence number of records in the page
     *
     * @return <array> - array of values
     * - start key which gives start sequence number
     * - end key which gives end sequence number
     */
    private RangeInfo getRecordRange() {
        return this.rangeInfo;
    }

    /**
     * Function to specify if previous page exists
     *
     * @return <Boolean> - true/false
     */
    public Boolean isPrevPageExists() {
        if (this.prevPageExists != null) {
            return this.prevPageExists;
        }
        return Boolean.TRUE;
    }

    /**
     * Function to specify if next page exists
     *
     * @return <Boolean> - true/false
     */
    public Boolean isNextPageExists() {
        if (this.nextPageExists != null) {
            return this.nextPageExists;
        }
        return Boolean.TRUE;
    }

    /**
     * calculates page range
     *
     * @param recordList - list of records which is show in current page
     * @return CarpoPagingModel  -
     */
    public RapidPagingModel calculatePageRange(List<?> recordList) {
        RangeInfo rangeInfo = new RangeInfo();
        int recordCount = recordList == null ? 0 : recordList.size();
        int pageLimit = this.getPageLimit();
        if (recordCount > 0) {
            //specifies what sequencce number of last record in prev page
            int prevPageLastRecordSequence = ((this.getCurrentPage() - 1) * pageLimit);

            rangeInfo.setStartIndex(prevPageLastRecordSequence + 1);
            if (rangeInfo.getStartIndex() == 1) {
                this.prevPageExists = false;
            }
            //Have less number of records than the page limit
            if (recordCount < pageLimit) {
                this.nextPageExists = false;
                rangeInfo.setEndIndex(prevPageLastRecordSequence + recordCount);
            } else {
                rangeInfo.setEndIndex(prevPageLastRecordSequence + pageLimit);
            }
            this.rangeInfo = rangeInfo;
        } else {
            //Disable previous page only if page is first page and no records exists
            if (this.getCurrentPage() == 1) {
                this.prevPageExists = false;
            }
            this.nextPageExists = false;

        }
        return this;
    }
}

class RangeInfo {

    private Integer startIndex;

    private Integer endIndex;

    public RangeInfo() {
    }

    public RangeInfo(Integer startIndex, Integer endIndex) {
        this.startIndex = startIndex;
        this.endIndex = endIndex;
    }

    public Integer getStartIndex() {
        return startIndex;
    }

    public void setStartIndex(Integer startIndex) {
        this.startIndex = startIndex;
    }

    public Integer getEndIndex() {
        return endIndex;
    }

    public void setEndIndex(Integer endIndex) {
        this.endIndex = endIndex;
    }
}